script_enemy_main{

let phase=rand_int(0,1);
let samecount=0;
let type=1;

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=10;
let spellcardnumber=58;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=0;
let frame=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SEshotb1=("\script\SoundEffects\shotb1.wav");

let BG1=("\script\Images\BackgroundLayers\Nightmare1.png");
let GRboss=("\script\Images\CharacterSprites\"~character~".png");

#include_function "script/Functions/SetSpellcardCommonData.txt";
#include_function "script/Functions/SpellcardNameLoad.txt";
#include_function "script/Functions/HealthBarLoad.txt";
#include_function "script/Functions/CutInLoad.txt";

@Initialize{
	LoadUserShotData("script\shots\ShotsNightmare1.txt");

	LoadSE("\script\SoundEffects\shotb1.wav");

	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare1.png");

	SetScore(1000000);
	SetLife(500);
	SetTimer(75);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	SetMovePosition02(cx,miny+100,50);
}
	
@MainLoop{

SetCollisionA(GetX,GetY,16);
SetCollisionB(GetX,GetY,16);
SetShotAutoDeleteClip(32,32,32,32);

if(GetCommonData("BombOn")==0){ damagerate=10; }
if(GetCommonData("BombOn")==1){ damagerate=0; }
SetDamageRate(damagerate,damagerate);

let difficulty="";
if(GetCommonDataDefault("Difficulty",2)==1){ difficulty="Easy"; }
if(GetCommonDataDefault("Difficulty",2)==2){ difficulty="Normal"; }
if(GetCommonDataDefault("Difficulty",2)==3){ difficulty="Hard"; }
if(GetCommonDataDefault("Difficulty",2)==4){ difficulty="Lunatic"; }

SpellcardName("Oneirology [Polysomnography]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

if(time%80==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+80,miny+120),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+80,miny+120),1.5);
	}
}


if(frame==90){
if(GetLife<300){ type=2; }
let xpos=minx;
	loop(9){
	CreateEnemyFromFile(GetCurrentScriptDirectory~"1 - Polysomnography - Familiar.txt",xpos,miny-rand(0,30),0,0,type);	
	xpos+=(maxx-minx)/8;
	}
phase++;
}

if(frame==270){
let xpos=minx+((maxx-minx)/16);
if(phase%2==0){ xpos-=(maxx-minx)/8; }
	loop(6){
	let speed=1.5;
		loop(3){
		CreateShot01(xpos,miny,speed,90,15,0);
		speed+=0.3;
		}
	xpos+=(maxx-minx)/4;
	}
frame=0;
PlaySE(SEshotb1);
}


time++; frame++;
if(usespell>0){ usespell--; } if(usespell<0){ usespell++; }
SetCommonData("Boss1X",GetX); SetCommonData("Boss1Y",GetY);

#include_function "script/Functions/SpellcardName.txt";
#include_function "script/Functions/HealthBar.txt";
#include_function "script/Functions/CutIn.txt";
}

@BackGround{
	if(bgfade<255){ bgfade+=5; }

	SetGraphicScale(1.5+1*cos(time/3),1.5);
	SetRenderState(ALPHA);
	SetAlpha(bgfade);
	SetColor(255,255,255);
	SetTexture(BG1);
	SetGraphicRect(0,0,900,300);
	SetGraphicAngle(0,0,0);
	DrawGraphic(cx,cy);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,0);
	SetColor(120,120,120);
	SetRenderState(ALPHA);

	if(usespell>=1){ SetGraphicRect(384,outfit,512,outfit+128); }
	if(usespell<=-1){ SetGraphicRect(512,outfit,640,outfit+128); }
	if(usespell==0){
		if(GetSpeedX<=0.5 && GetSpeedX>=-0.5){ SetGraphicRect(0,outfit,128,outfit+128); }
		else if(GetSpeedX<-0.5){ SetGraphicRect(128,outfit,256,outfit+128); }
		else if(GetSpeedX>0.5){ SetGraphicRect(256,outfit,384,outfit+128); }
	}
	DrawGraphic(GetX,GetY);
}

@Finalize{
	SetCommonData("NightmareSpellcards",2);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}